package com.fish.web.controller;

import com.fish.common.result.Result;
import com.fish.model.vo.FileInfoVO;
import com.fish.service.FileUploadService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 文件上传控制器
 */
@Tag(name = "文件管理", description = "文件上传、下载、删除等相关接口")
@Slf4j
@RestController
@RequestMapping("/file")
@RequiredArgsConstructor
public class FileUploadController {
    
    private final FileUploadService fileUploadService;
    
    /**
     * 上传单个文件
     * 
     * @param file 文件
     * @param path 存储路径（可选）
     * @param storageType 存储类型（minio/local/oss，默认minio）
     * @return 文件信息
     */
    @Operation(summary = "上传单个文件", description = "上传单个文件到指定存储")
    @Parameters({
        @Parameter(name = "file", description = "文件", required = true, content = @Content(mediaType = "multipart/form-data")),
        @Parameter(name = "path", description = "存储路径", required = false, example = "images"),
        @Parameter(name = "storageType", description = "存储类型(minio/local/oss)", required = false, example = "minio")
    })
    @PostMapping("/upload")
    public Result<FileInfoVO> upload(
            @RequestParam("file") MultipartFile file,
            @RequestParam(value = "path", required = false) String path,
            @RequestParam(value = "storageType", required = false, defaultValue = "minio") String storageType) {
        
        log.info("开始上传文件: {}, 存储类型: {}", file.getOriginalFilename(), storageType);
        FileInfoVO fileInfoVO = fileUploadService.upload(file, path, storageType);
        return Result.success(fileInfoVO);
    }
    
    /**
     * 批量上传文件
     * 
     * @param files 文件列表
     * @param path 存储路径（可选）
     * @param storageType 存储类型
     * @return 文件信息列表
     */
    @Operation(summary = "批量上传文件", description = "批量上传多个文件")
    @Parameters({
        @Parameter(name = "files", description = "文件列表", required = true, content = @Content(mediaType = "multipart/form-data")),
        @Parameter(name = "path", description = "存储路径", required = false),
        @Parameter(name = "storageType", description = "存储类型", required = false)
    })
    @PostMapping("/batch-upload")
    public Result<List<FileInfoVO>> batchUpload(
            @RequestParam("files") List<MultipartFile> files,
            @RequestParam(value = "path", required = false) String path,
            @RequestParam(value = "storageType", required = false, defaultValue = "minio") String storageType) {
        
        log.info("开始批量上传文件，数量: {}", files.size());
        List<FileInfoVO> fileInfoList = fileUploadService.batchUpload(files, path, storageType);
        return Result.success(fileInfoList);
    }
    
    /**
     * 删除文件
     * 
     * @param fileId 文件ID
     * @return 删除结果
     */
    @Operation(summary = "删除文件", description = "根据文件ID删除文件")
    @Parameter(name = "fileId", description = "文件ID", required = true, example = "1")
    @DeleteMapping("/{fileId}")
    public Result<Boolean> delete(@PathVariable Long fileId) {
        log.info("删除文件, ID: {}", fileId);
        boolean result = fileUploadService.delete(fileId);
        return Result.success(result);
    }
    
    /**
     * 获取文件信息
     * 
     * @param fileId 文件ID
     * @return 文件信息
     */
    @Operation(summary = "获取文件信息", description = "根据文件ID查询文件信息")
    @Parameter(name = "fileId", description = "文件ID", required = true, example = "1")
    @GetMapping("/{fileId}")
    public Result<FileInfoVO> getFileInfo(@PathVariable Long fileId) {
        FileInfoVO fileInfoVO = fileUploadService.getFileInfo(fileId);
        return Result.success(fileInfoVO);
    }
    
    /**
     * 获取用户上传的文件列表
     * 
     * @param userId 用户ID
     * @return 文件列表
     */
    @Operation(summary = "获取用户文件列表", description = "查询指定用户上传的所有文件")
    @Parameter(name = "userId", description = "用户ID", required = true, example = "1")
    @GetMapping("/user/{userId}")
    public Result<List<FileInfoVO>> getUserFiles(@PathVariable Long userId) {
        List<FileInfoVO> fileList = fileUploadService.getUserFiles(userId);
        return Result.success(fileList);
    }
}
